package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.CustomerChartQueryObject;
import cn.wolfcode.crm.service.ICustomerChartService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerChart")
public class CustomerChartController {
    @Autowired
    private ICustomerChartService chartService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerChartQueryObject qo, Model model)
    {
        System.out.println("===="+chartService.selectCustomerChart(qo));
        model.addAttribute("charts",chartService.selectCustomerChart(qo));
        model.addAttribute("groupTypeName",qo.getGroupTypeName());
        return "/chart/list";
    }

    @RequestMapping("/chartBar")
    public String charBar(@ModelAttribute("qo") CustomerChartQueryObject qo, Model model)
    {
        model.addAttribute("groupTypeName",qo.getGroupTypeName());
        //将查询结果封装到一个集合中
        List<Map<String, Object>> list = chartService.selectCustomerChart(qo);
        List<String> groupTypes = new ArrayList<>();
        List<Object> numbers = new ArrayList<>();
        for (Map<String, Object> map : list) {
            groupTypes.add(map.get("groupType").toString());
            numbers.add(map.get("number"));
        }
        System.out.println(JSON.toJSONString(groupTypes));
        System.out.println(JSON.toJSONString(numbers));
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("numbers", JSON.toJSONString(numbers));
        return "/chart/index";
    }

    @RequestMapping("/chartPie")
    public String charPie(@ModelAttribute("qo") CustomerChartQueryObject qo, Model model)
    {
        model.addAttribute("groupTypeName",qo.getGroupTypeName());
        List<String> groupTypes = new ArrayList<>();
        //将查询结果封装到一个集合中
        List<Map<String, Object>> list = chartService.selectCustomerChart(qo);
        List<Map<String, Object>> datas = new ArrayList<>();
        List<Object> numbers = new ArrayList<>();
        for (Map<String, Object> map : list) {
            groupTypes.add(map.get("groupType").toString());

            Map<String, Object> data = new HashMap<>();
            data.put("name",map.get("groupType"));
            data.put("value",map.get("number"));
            datas.add(data);
        }
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("datas", JSON.toJSONString(datas));
        return "/chart/chartPie";
    }
}
